/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function(){
  //submit dei post  
  $("#submit_form").click(function(){
    $.ajax({
      data: $(this).parents("form:first").serialize(),
      success: function(response){
        if(!response.match(/\</)){
          response = JSON.parse(response);
          var error = "";
          for(var i=0;i<response.length;i++){
            if(response[i].description != undefined)
              error += response[i].description+"<br/>";
          }
          Dialog(error);
        }
        else{
          Dialog("Contenuto inserito con successo");
          setTimeout(function(){
            window.location.reload();
          }, 2000);
        }
        centerElement("#bg-message");
      }
    })
  //$(this).parents("form:first").submit();
  });
  
  //update dei post
  $("#update").click(function(){
    $(this).parents("form:first").submit();
  });
  
  //eliminazione dei post
  $(".elimina-post").click(function(){
    var del =confirm("Sei sicuro di voler elimanare l'elemento?");
    if (del==true){
      $(this).parents("form:first").submit();
    } else{
      return;
    }
  });
});

function Dialog(str){
  var url = getBaseURL();
  $.blockUI.defaults.css = {};
  $.blockUI({
    css: {
      "top":"5%",
      "left":"25%",
      "cursor":"normal"
    },
    message:"<div id='bg-message'><div id='modal-close' title='Chiudi'></div>"+str+"</div>",
    onBlock:function(){
      $("#modal-close").css({
        "background":"url('"+url+"pontedicarta/wp-content/themes/PontediCarta/img/modal-close.png') 0 0 transparent",
        "width":"30px",
        "height":"30px",
        "cursor":"pointer",
        "position":"absolute",
        "top":"-10px",
        "left":"460px"
      });
      $("#modal-close").mouseenter(function(){
        $(this).css({
          "background-position":" 0 -30px"
        });
      });
      $("#modal-close").mouseleave(function(){
        $(this).css({
          "background-position":" 0 0"
        });
      });
      $("#modal-close").click(function(){
        $(".blockOverlay").trigger("click");
      })
      $("#bg-message").css({
        "background-color": "#96414D",
        "padding": "40px 150px",
        "color": "white",
        "-webkit-border-radius": "5px",
        "-moz-border-radius": "5px"
      });
      $('#modal-cphiudi').attr('title','Clicca per sbloccare').click(function(evt){
        evt.preventDefault();
        $.unblockUI();
      });
      $('.blockOverlay').click(function(){
        $.unblockUI();
      });
    }
  });
} 

function centerElement(element){
  var perc = parseInt((100*$(element).width())/$(document).width());
  perc = 100-perc;
  var left = 0;
  if(perc > 0) left=parseInt(perc/2);
  else left = 0;
  $(element).css({
    "left":left+"%"
  });
  $(window).resize(function(){
    var perc = parseInt((100*$(element).width())/$(document).width());
    perc = 100-perc;
    var left =0;
    if(perc > 0) left=parseInt(perc/2);
    else left = 0;
    $(element).css({
      "left":left+"%"
                                
    });
    return;
  });
}

function getBaseURL() {
  var url = location.href;  // entire url including querystring - also: window.location.href;
  var baseURL = url.substring(0, url.indexOf('/', 14));


  if (baseURL.indexOf('http://localhost') != -1) {
    // Base Url for localhost
    var url = location.href;  // window.location.href;
    var pathname = location.pathname;  // window.location.pathname;
    var index1 = url.indexOf(pathname);
    var index2 = url.indexOf("/", index1 + 1);
    var baseLocalUrl = url.substr(0, index2);

    return baseLocalUrl + "/";
  }
  else {
    // Root Url for domain name
    return baseURL + "/";
  }

}
